package com.orcsystem.product.web;

import com.orcsystem.product.entity.CategoryEntity;
import com.orcsystem.product.service.CategoryService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @CreateData: 2020/11/18-14:38
 * Author   AiHongSen
 * 方法说明
 * SL 查询、DL 删除、UP 更新、IN 新增方法
 * IndexController->作用:
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redis;

    @GetMapping(value = {"/","index","index.html"})
    public String indexPage(Model model){
        // 通过视图解析器 ThymeleafProperties 增加前缀 classpath:/templates/  后缀 .html
        // TODO 1.分类数据
        long start = System.currentTimeMillis();
        List<CategoryEntity> categorys = categoryService.getLevel1Categorys();
        model.addAttribute("level1categorys",categorys);
        long end = System.currentTimeMillis();
        return "index";
    }
    @GetMapping("/getNums")
    @ResponseBody
    @Cacheable(cacheNames = "s-nums",key = "#root.method.name")
    public Map<String,Integer> nums(){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("A",1);
        map.put("B",2);
        return map;
    }

    @ResponseBody
    @GetMapping("/test")
    public String test(){
        RLock lock = redisson.getLock("y-Lock");
        // 最常见的使用方法
        lock.lock();
        // RLock lock = redisson.getLock("TranLock");
        try {
            System.out.println(" 加锁成功 ");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println(" 解锁成功 ");
            lock.unlock();
        }
        String name = Thread.currentThread().getName();
        return name;
    }

    /**
     * 读写锁
     * getReadWriteLock()
     */

}
